# 遍历文件夹下所有目录
import os
# 引入 os 模块
from code11_2 import *

class FileDetail(Component):
    """文件详情"""

    def __init__(self, name):
        super().__init__(name)
        self._size = 0
    
    def setSize(self, size):
        self._size = size

    def getFileSize(self):
        return self._size

    def feature(self, indent):
        # 文件大小，单位：KB，精度：2位小数
        fileSize = round(self._size / float(1024), 2)
        print("文件名称：%s, 文件大小：%sKB" % (self._name, fileSize))


class FolderDetail(Composite):
    """文件夹详情"""

    def __init__(self, name):
        super().__init__(name)
        self._count = 0

    def setCount(self, fileNum):
        self._count = fileNum

    def getCount(self):
        return self._count

    def feature(self, indent):
        print("文件夹名：%s，文件数量：%d。包含的文件：" % (self._name, self._count))
        super().feature(indent)


def scanDir(rootPath, folderDetail):
    """扫描某一文件夹下的所有目录"""

    if not os.path.isdir(rootPath):
        raise ValueError("rootPath 不是有效的路径：%s" % rootPath)

    if folderDetail is None:
        raise ValueError("folderDetail 不能为空！")

    fileNames = os.listdir(rootPath)
    for fileName in fileNames:
        filePath = os.path.join(rootPath, fileName)
        if os.path.isdir(filePath):
            folder = FolderDetail(fileName)
            scanDir(filePath, folder)
            folderDetail.addComponent(folder)
        else:
            fileDetail = FileDetail(fileName)
            fileDetail.setSize(os.path.getsize(filePath))
            folderDetail.addComponent(fileDetail)
            folderDetail.setCount(folderDetail.getCount() + 1)


def testDir():
    folder = FolderDetail("生活中的设计模式")
    scanDir("/home/jacek/Pictures", folder)
    folder.feature("")

testDir()
